<template>
  <div class="container">
    <el-card class="box-card" shadow="always">
      <el-row :gutter="24">
        <el-form inline @submit.native.prevent="search">
          <el-col :span="7">
            <el-form-item>
              <el-input
                v-model="searchForm.queryConditions"
                clearable
                placeholder="查询供应商名"
                prefix-icon="el-icon-search"
                size="medium"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item>
              <el-select
                v-model="searchForm.round"
                clearable
                placeholder="所处轮次"
                @change="roundSelected"
              >
                <el-option value="1" label="一轮竞价" />
                <el-option value="2" label="二轮竞价" />
                <el-option value="3" label="三轮竞价" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item>
              <el-select
                v-model="searchForm.productId"
                placeholder="竞标产品"
                @change="productSelected"
                clearable
              >
                <el-option
                  v-for="item in products"
                  :key="item.id"
                  :value="item.id"
                  :label="item.productName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-button native-type="submit" size="medium" type="primary">
                搜索
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <div>
        <el-table
          :data="list"
          border
          style="width: 100%"
          v-loading="recordsLoading"
        >
          <el-table-column
            type="index"
            width="50"
            label="编号"
            align="center"
          />
          <el-table-column prop="productName" label="竞价产品" align="center" />

          <el-table-column label="报价" align="center">
            <template slot-scope="scope">
              {{ scope.row.price }}{{ scope.row.bidUnit }}
            </template>
          </el-table-column>
          <el-table-column label="可供数量" align="center">
            <template slot-scope="scope">
              {{ scope.row.tenderProvide }}{{ provideUnit(scope.row.bidUnit) }}
            </template>
          </el-table-column>
          <el-table-column prop="bidCount" label="所处轮次" align="center" />

          <el-table-column prop="supplier" label="供应商" align="center" />

          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="200"
          >
            <template slot-scope="scope">
              <el-tooltip content="查看更多报价" placement="top">
                <el-button
                  type="primary"
                  size="mini"
                  icon="el-icon-search"
                  circle
                  @click="showDialog(scope.row)"
                />
              </el-tooltip>

              <el-tooltip
                content="选定"
                placement="top"
                v-if="scope.row.status === 0"
              >
                <el-button
                  type="success"
                  size="mini"
                  icon="el-icon-thumb"
                  circle
                  @click="selectSupplier(scope.row)"
                />
              </el-tooltip>

              <el-tooltip content="撤销" placement="top" v-else>
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-back"
                  circle
                  @click="revokeSelect(scope.row)"
                />
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          :current-page="paging.current"
          :page-sizes="paging.pageSizes"
          :page-size="paging.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paging.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <Dialog
      :visible="recordsVisible"
      :records="recordsInfos"
      @cancel="cancel"
    />

    <edit-dialog
      :index="index"
      :title="dialogTitle"
      :visible="editDialogVisible"
      :price-info="priceInfo"
      @cancel="cancel"
      @ok="ok"
    />
  </div>
</template>

<script>
import {
  fixTender,
  revokeSelect,
  getAllCompeteInfos,
  getCompeteTenderRecords,
} from "@/api/purchaserTender";
import Dialog from "./Dialog";
import EditDialog from "./editDialog.vue";
export default {
  name: "CompeteRecords",
  components: { Dialog, EditDialog },
  props: {
    products: {
      type: Array,
    },
    tenderId: {
      type: String,
    },
  },
  data() {
    return {
      searchForm: {
        queryConditions: "",
        round: "",
        productId: "",
      },
      list: [],
      paging: {
        total: 0,
        current: 1,
        pageSizes: [5, 10, 15],
        size: 5,
      },
      recordsInfos: [],
      recordsLoading: false,
      recordsVisible: false,
      editDialogVisible: false,
      dialogTitle: "",
      index: 0,
      priceInfo: {},
    };
  },

  //   created() {
  //     this.recordsLoading = true;
  //     this.getData();
  //   },
  mounted() {
    this.$nextTick(() => {
      this.recordsLoading = true;
      this.getData();
    });
  },
  methods: {
    provideUnit(val) {
      return val.replace("/元", "");
    },
    selectSupplier(row) {
      this.priceInfo = this.extendCopy(row);
      this.index = 1;
      this.dialogTitle = "选定供应商";
      this.editDialogVisible = true;
    },
    revokeSelect(row) {
      const data = {
        competeId: row.id,
        bid: row.price + row.bidUnit,
        provide: row.tenderProvide + row.bidUnit.replace("/元", ""),
        supplierId: row.supplierId,
        accountId: this.$store.getters.accountId,
        tenderId: row.tenderId,
        productId: row.productId,
      };
      revokeSelect(data).then((res) => {
        if (res.code === 200) {
          this.getData();
          this.$message.warning("已撤回定标结果");
        }
      });
    },
    productSelected(val) {
      this.searchForm.productId = val;
      this.search();
    },
    roundSelected(val) {
      this.searchForm.round = val;
      this.search();
    },
    search() {
      this.paging.current = 1;
      this.recordsLoading = true;
      this.getData();
    },
    getData() {
      const form = {
        ...this.searchForm,
        tenderId: this.tenderId,
        page: this.paging.current,
        pageSize: this.paging.size,
      };
      getCompeteTenderRecords(form)
        .then((res) => {
          if (res.data) {
            this.list = res.data.infos;
            // 返还每页显示的条数
            //this.paging.size = res.data.size;
            this.paging.total = res.data.totalCount;
            this.recordsLoading = false;
          }
        })
        .catch((err) => {
          // 如果没有数据就显示为空
          this.list = [];
          // 返还每页显示的条数
          //this.paging.size = res.data.size;
          this.paging.total = 0;
          this.recordsLoading = false;
        });
    },
    handleSizeChange(val) {
      this.paging.current = 1;
      this.paging.size = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.paging.current = val;
      this.getData();
    },
    showDialog(row) {
      this.index = 0;
      this.recordsVisible = true;
      const data = {
        round: row.bidCount,
        tenderId: row.tenderId,
        productId: row.productId,
      };
      getAllCompeteInfos(data).then((res) => {
        if (res.code === 200) {
          this.recordsInfos = res.data;
        }
      });
    },

    // 对象的深拷贝
    // 对象进行深拷贝，否则“编辑”框内修改内容也会影响到页面
    extendCopy(p) {
      var c = {};
      for (var i in p) {
        c[i] = p[i];
      }
      return c;
    },

    cancel() {
      if (this.index === 0) {
        this.recordsVisible = false;
      } else if (this.index === 1) {
        this.editDialogVisible = false;
      }
    },
    async ok(val) {
      fixTender(val).then((res) => {
        if (res.code === 200) {
          this.getData();
          this.index = 0;
          this.$message.success("已为竞标产品选定供应商");
          this.editDialogVisible = false;
        }
        this.$emit("refresh");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
.btn {
  float: right;
  margin-left: 20px;
}
</style>
